using System;
using Amazon.ECS.Model;
using RomanJendrusz.Amazon.ETL.Domain;
using System.Collections.Generic;
namespace RomanJendrusz.Amazon.ETL.Messages.MessageMappers
{
    public class GetSimiliarBooksMessageMapper: MessageMapper<GetSimiliarBooksRequest, ItemLookupRequest, ItemLookupResponse>
    {
        public override AmazonResponse MapToAmazonResponse(ItemLookupResponse awsResponse)
        {
            var bookItem = GetRequestItem(awsResponse,0);
            if(bookItem.SimilarProducts == null || bookItem.SimilarProducts.SimilarProduct == null)
            {
                throw new InvalidOperationException("Can't find the similiar books");
            }
            var books = new List<Book>();
            foreach(var book in bookItem.SimilarProducts.SimilarProduct)
            {
                books.Add(new Book(){Asin=book.ASIN, Title=book.Title});
            }
            return new GetSimiliarBooksResponse(CorrelationId){SimiliarBooks=books};
        }


        public override ItemLookupRequest MapToAwsRequest(GetSimiliarBooksRequest request)
        {
            var awsRequest = new ItemLookupRequest();
            awsRequest.ItemId.Add(request.BookAsin);
            //awsRequest.SearchIndex = "Books";
            //awsRequest.IdType = "ISBN";
            awsRequest.ResponseGroup.Add(request.ResponseGroup);
            awsRequest.MerchantId =request.MerchantId;
            return awsRequest;
        }
    }
}